package com.ksd.pug.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ksd.pug.commons.anno.PugLog;
import com.ksd.pug.commons.enums.ResultStatusEnumA;
import com.ksd.pug.commons.ex.PugBussinessException;
import com.ksd.pug.commons.utils.fn.asserts.Vsserts;
import com.ksd.pug.controller.BaseController;
import com.ksd.pug.pojo.Permission;
import com.ksd.pug.pojo.SysLoginUser;
import com.ksd.pug.service.user.ISysLoginUserService;
import com.ksd.pug.vo.RoleUserVo;
import com.ksd.pug.vo.TreeVo;
import com.ksd.pug.vo.user.UserVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: hcl
 * @date: 2022/1/24 15:06
 * @Description:
 */
@RestController
@RequiredArgsConstructor
@Slf4j
public class UserController extends BaseController {

    private final ISysLoginUserService userService;

    /**
     * 查询用户列表信息并分页
     *
     * @return
     */
    @PostMapping("/user/list")
    public IPage<SysLoginUser> findUserPage(@RequestBody UserVo userVo) {
        return userService.findUserPage(userVo);
    }


    /**
     * 根据用户id删除用户
     *
     * @param id
     * @return
     */
    @PostMapping("/user/del/{id}")
    public boolean delUser(@PathVariable("id") Long id) {
        return userService.delUser(id);
    }

    /**
     * 根据用户ids批量删除用户
     *
     * @param UserVo
     * @return
     */
    @PostMapping("/user/delBatch")
    public boolean delUser(@RequestBody UserVo UserVo) {
        log.info("你要批量删除的IDS是:{}", UserVo.getBatchIds());
        if (Vsserts.isEmpty(UserVo.getBatchIds())) {
            throw new PugBussinessException(ResultStatusEnumA.EMPTY_IDS_VALID);
        }
        return userService.delBatchUser(UserVo.getBatchIds());
    }
    /**
     * 根据用户id查询用户信息
     * @param id
     * @return
     */
    @GetMapping("/user/get/{id}")
    public SysLoginUser getUser(@PathVariable("id") Long id) {
        log.info("用户id是:{}",id);
        return userService.getUser(id);
    }

    /**
     * 添加或删除用户
     *
     * @param user
     * @return
     */
    @PostMapping("/user/saveorupdate")
    @PugLog(title = "修改个人信息")
    public SysLoginUser saveUpdateUser(@RequestBody  SysLoginUser user) {
        return userService.saveUpdateUser(user);
    }


    /**
     * 查询用户权限
     *
     * @param userId
     * @return
     */
    @PostMapping("/user/findPermission/{userid}")
    public List<Permission> findBySysPermissionUserId(@PathVariable("userid") Long userId) {
        return userService.findBySysPermissionUserId(userId);
    }

    /**
     * 查询用户权限树形
     *
     * @param userId
     * @return
     */
    @PostMapping("/user/permissionTree/{userid}")
    public List<TreeVo> getTreeList(@PathVariable("userid") Long userId) {
        return userService.getTreeList(userId);
    }

    /**
     * 添加用户对应的角色
     *
     * @param roleUserVo
     * @return
     */
    @PostMapping("/user/saveRole")
    public boolean saveUserRole(@RequestBody RoleUserVo roleUserVo) {
        return userService.saveUserRole(roleUserVo);
    }
}
